import api from '../../api';
import { message } from 'antd';
export default {
  namespace: 'monitoringM',

  state: {
    selectData: [],
    tableDataObj: {}, //列表数据
    detailData: [],
    isLoading: false, //第一次点击查询按钮有loading效果
  },

  effects: {
    //获取风控条目下拉
    *getSelectData({ payload }, { call, put }) {
      const response = yield call(api.monitoring.getSelectData, payload);
      if (response && response.success == true) {
        const arr = [];
        if (response.data.data) {
          response.data.data.map((item) => {
            arr.push({
              title: item.ruleDesc,
              value: item.ruleId,
            });
          });
        }

        yield put({
          type: 'save',
          payload: {
            selectData: arr,
          },
        });
      } else {
        message.error('风控条目下拉列表查询失败!');
      }
    },
    // 获取表格数据
    *getTableDta({ payload, callback }, { call, put, select }) {
      const response = yield call(api.monitoring.getTableDta, payload);
      let tableDataObj = yield select((state) => state.monitoringM.tableDataObj);
      if (response) {
        if (response.code == 0) {
          if (!payload.type && tableDataObj.dataSource && tableDataObj.dataSource.length) {
            const newData = response.data.dataSource;
            newData.map((item) => {
              //更新数值
              tableDataObj.dataSource.map((x) => {
                if (x.ruleNo == item.ruleNo) {
                  x.warningValue = item.warningValue;
                  x.marginValue = item.marginValue;
                  x.value = item.value;
                  x.prohibitValue = item.prohibitValue;
                  x.ruleId = item.ruleId;
                  x.numberOfAlarms = item.numberOfAlarms;
                }
              });
            });
            const oldList = tableDataObj.dataSource.map((item) => {
              //将原数组id集合成新的数组
              return item.ruleNo;
            });
            const result = newData.filter((item) => !oldList.includes(item.ruleNo)); //过滤出新的元素
            result.map((item) => {
              //添加新增的元素
              tableDataObj.dataSource.unshift(item);
            });
            yield put({
              //为了让页面层数据更新，触发了model
              type: 'save',
              payload: {
                tableDataObj,
                isLoading: false,
              },
            });
            return;
          }
          yield put({
            type: 'save',
            payload: {
              tableDataObj: response.data,
              isLoading: false,
            },
          });
        } else {
          message.error(response.msg || '限额监控列表查询失败!');
        }
      } else {
        message.error('限额监控列表查询失败!');
      }
      if (payload.type) {
        yield put({
          type: 'save',
          payload: {
            isLoading: false,
          },
        });
      }
    },

    *getDetails({ payload, callback }, { call, put }) {
      const response = yield call(api.monitoring.getDetails, payload);
      if (response && response.code == 0) {
        yield put({
          type: 'save',
          payload: {
            detailData: response,
          },
        });
        callback(response);
      } else {
        message.error('查询失败');
      }
    },

    *runAll({ payload, callback }, { call, put }) {
      yield call(api.monitoring.runAll, payload);
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
